package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.vo.*;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.autoconfig.template.AipFaceTemplate;
import com.tanhua.autoconfig.template.OssTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.UserLikeApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.UserLike;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

/**
 * @author ck
 * @date 2021/11/11 11:23
 */

@Service
public class UserInfoService {

    @DubboReference
    private UserApi userApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private OssTemplate ossTemplate;

    @Autowired
    private AipFaceTemplate aipFaceTemplate;

    @DubboReference
    private UserLikeApi userLikeApi;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    //保存用户信息
    public void save(UserInfo userInfo) {
        userInfoApi.save(userInfo);
    }


    //更新用户头像
    public void updateHead(MultipartFile headPhoto, Long id) throws IOException {
        //1、将图片上传到oss
        String imageUrl = ossTemplate.upload(headPhoto.getOriginalFilename(), headPhoto.getInputStream());
        //2、调用百度云判断是否包含人脸
        boolean detect = aipFaceTemplate.detect(imageUrl);

        if (!detect) {
            //2.1 如果不包含 抛出异常
            ossTemplate.delete(imageUrl);
            throw new BusinessException(ErrorResult.faceError());
        } else {
            //2.2 包含，调用api更新
            UserInfo userInfo = new UserInfo();
            userInfo.setId(Long.valueOf(id));
            userInfo.setAvatar(imageUrl);
            userInfoApi.update(userInfo);
        }
    }

    //根据id查询
    public UserInfoVo findById(Long id) {
        UserInfo userInfo = userInfoApi.findById(id);

        UserInfoVo userInfoVo = new UserInfoVo();

        BeanUtils.copyProperties(userInfo, userInfoVo); //copy同名同类型属性

        if (userInfo.getAge() != null) {
            userInfoVo.setAge(userInfo.getAge().toString());
        }

        return userInfoVo;
    }

    public void update(UserInfo userInfo) {
        userInfoApi.update(userInfo);
    }

    //    互相喜欢，喜欢，粉丝 - 统计
    public EachLoveFanVo counts() {
        Integer a = 0;
        Integer b = 0;
        Integer c = 0;
        //通过redis缓存获取当前用户喜欢的用户id
        Set<String> members = redisTemplate.opsForSet().members(Constants.USER_LIKE_KEY + UserHolder.getUserId());
        b = members.size();

        //通过当前用户获取当前用户喜欢或者被喜欢的所有数据
        List<UserLike> list = userLikeApi.find(UserHolder.getUserId());
        for (UserLike userLike : list) {
            Set<String> set = redisTemplate.opsForSet().members(Constants.USER_LIKE_KEY + userLike.getUserId());
            if (set != null) {
                if (set.contains(UserHolder.getUserId().toString())) {
                    c++;
                }
                if (set.contains(UserHolder.getUserId().toString()) && members.contains(userLike.getUserId().toString())) {
                    a++;
                }
            }
        }
        return new EachLoveFanVo(a, b, c);
    }


    /**
     * 互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     * 1 互相关注
     * 2 我关注
     * 3 粉丝
     * 4 谁看过我
     */
    public PageResult findList(String type, Integer page, Integer pagesize, String nickname) {
        //通过redis缓存获取当前用户喜欢的用户id
        Set<String> members = redisTemplate.opsForSet().members(Constants.USER_LIKE_KEY + UserHolder.getUserId());
        //通过当前用户获取当前用户喜欢或者被喜欢的所有数据
        List<UserLike> list = userLikeApi.find(UserHolder.getUserId());

        //获取粉丝的数据
        List<UserLike> fans = userLikeApi.findFans(UserHolder.getUserId());

        List<UserInfoVooo> vos = new ArrayList<>();
        for (UserLike userLike : list) {
//            Set<String> set = redisTemplate.opsForSet().members(Constants.USER_LIKE_KEY + userLike.getUserId());
            Set<String> set = redisTemplate.opsForSet().members(Constants.USER_LIKE_KEY + userLike.getUserId());
            //粉丝数据
            if (!CollUtil.isEmpty(set)) {
                if (type.equals("3")) {
                    if (set.contains(UserHolder.getUserId().toString())) {
                        for (UserLike fan : fans) {
                            UserInfo userInfo = userInfoApi.findById(fan.getUserId());
                            UserInfoVooo vo = UserInfoVooo.init(userInfo);
                            vo.setAlreadyLove(false);
                            vos.add(vo);
                        }
                        return new PageResult(page, pagesize, 0l, vos);
                    }
                } else if (type.equals("2")) {
                    if (members.contains(userLike.getUserId().toString())) {
                        for (String member : members) {
                            long loveId = Long.parseLong(member);
                            UserInfo userInfo = userInfoApi.findById(loveId);
                            UserInfoVooo vo = UserInfoVooo.init(userInfo);
                            vos.add(vo);
                        }
                        return new PageResult(page, pagesize, 0l, vos);
                    }
                } else {
                    if (set.contains(UserHolder.getUserId().toString()) && members.contains(userLike.getUserId().toString())) {
                        UserInfo userInfo = userInfoApi.findById(userLike.getUserId());
                        UserInfoVooo vo = UserInfoVooo.init(userInfo);
                        vos.add(vo);
                    }
                }
            }
        }
        return new PageResult(page, pagesize, 0l, vos);
    }
}

